package cn.net.yugu.doraemon.sharding.utils;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.ResolvableType;

/**
 * @author wanghongli
 * @date 2023/12/16 15:34
 * @description ShardSpringBeanUtil
 **/
public class YgShardSpringBeanUtil implements ApplicationContextAware, InitializingBean {
    private ApplicationContext applicationContext;
    private static YgShardSpringBeanUtil ygShardSpringBeanUtil;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        ygShardSpringBeanUtil = this;
    }

    public static Object getBean(String name) throws BeansException {
        return ygShardSpringBeanUtil.applicationContext.getBean(name);
    }

    public static <T> T getBean(String name, Class<T> requiredType) throws BeansException {
        return ygShardSpringBeanUtil.applicationContext.getBean(name, requiredType);
    }

    public static Object getBean(String name, Object... args) throws BeansException {
        return ygShardSpringBeanUtil.applicationContext.getBean(name, args);
    }

    public static <T> T getBean(Class<T> requiredType) throws BeansException {
        return ygShardSpringBeanUtil.applicationContext.getBean(requiredType);
    }

    public static <T> T getBean(Class<T> requiredType, Object... args) throws BeansException {
        return ygShardSpringBeanUtil.applicationContext.getBean(requiredType, args);
    }

    public static <T> ObjectProvider<T> getBeanProvider(Class<T> requiredType) {
        return ygShardSpringBeanUtil.applicationContext.getBeanProvider(requiredType);
    }

    public static <T> ObjectProvider<T> getBeanProvider(ResolvableType requiredType) {
        return ygShardSpringBeanUtil.applicationContext.getBeanProvider(requiredType);
    }

    public static boolean containsBean(String name) {
        return ygShardSpringBeanUtil.applicationContext.containsBean(name);
    }

    public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
        return ygShardSpringBeanUtil.applicationContext.isSingleton(name);
    }

    public static boolean isPrototype(String name) throws NoSuchBeanDefinitionException {
        return ygShardSpringBeanUtil.applicationContext.isPrototype(name);
    }

    public static boolean isTypeMatch(String name, ResolvableType typeToMatch) throws NoSuchBeanDefinitionException {
        return ygShardSpringBeanUtil.applicationContext.isTypeMatch(name, typeToMatch);
    }

    public static boolean isTypeMatch(String name, Class<?> typeToMatch) throws NoSuchBeanDefinitionException {
        return ygShardSpringBeanUtil.applicationContext.isTypeMatch(name, typeToMatch);
    }

    public static Class<?> getType(String name) throws NoSuchBeanDefinitionException {
        return ygShardSpringBeanUtil.applicationContext.getType(name);
    }

    public static Class<?> getType(String name, boolean allowFactoryBeanInit) throws NoSuchBeanDefinitionException {
        return ygShardSpringBeanUtil.applicationContext.getType(name, allowFactoryBeanInit);
    }

    public static String[] getAliases(String name) {
        return ygShardSpringBeanUtil.applicationContext.getAliases(name);
    }
}
